import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import Vertex from './vertex.glsl'
import Fragment from './fragment.glsl'

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight
)
camera.position.set(0, 0, 4)
scene.add(camera)

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(window.innerWidth, window.innerHeight)

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

const axHelp = new THREE.AxesHelper(5)
scene.add(axHelp)

const geometry = new THREE.PlaneGeometry(3, 3, 32, 32)
const material = new THREE.RawShaderMaterial({
  vertexShader: Vertex,
  fragmentShader: Fragment,
  side: THREE.DoubleSide,
})

const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)

document.body.appendChild(renderer.domElement)

const handleTick = () => {
  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(handleTick)
}

const handleSize = () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()

  renderer.setSize(window.innerWidth, window.innerHeight)
}

window.addEventListener('resize', handleSize)
handleTick()
